{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import matplotlib.pyplot as plt \n",
    "import pandas as pd\n",
    "from mpl_toolkits.mplot3d import Axes3D\n",
    "from scipy.interpolate import griddata\n",
    "from matplotlib import cm"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 让图片中可以显示中文\n",
    "plt.rcParams['font.sans-serif'] = 'SimHei'\n",
    "# 让图片中可以显示负号\n",
    "plt.rcParams['axes.unicode_minus'] = False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>P1</th>\n",
       "      <th>P5</th>\n",
       "      <th>P7</th>\n",
       "      <th>P8</th>\n",
       "      <th>P9</th>\n",
       "      <th>P10</th>\n",
       "      <th>P11</th>\n",
       "      <th>P12</th>\n",
       "      <th>P13</th>\n",
       "      <th>P14</th>\n",
       "      <th>...</th>\n",
       "      <th>P29</th>\n",
       "      <th>P30</th>\n",
       "      <th>P31</th>\n",
       "      <th>P32</th>\n",
       "      <th>P33</th>\n",
       "      <th>P34</th>\n",
       "      <th>P35</th>\n",
       "      <th>P36</th>\n",
       "      <th>P37</th>\n",
       "      <th>P38</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0.5144</td>\n",
       "      <td>1.5</td>\n",
       "      <td>13768.680664</td>\n",
       "      <td>286696.812500</td>\n",
       "      <td>28747.720703</td>\n",
       "      <td>354003.71875</td>\n",
       "      <td>34314.933594</td>\n",
       "      <td>357846.5000</td>\n",
       "      <td>56136.917969</td>\n",
       "      <td>395813.1875</td>\n",
       "      <td>...</td>\n",
       "      <td>97280.148438</td>\n",
       "      <td>371894.75000</td>\n",
       "      <td>96071.804688</td>\n",
       "      <td>368650.34375</td>\n",
       "      <td>59487.933594</td>\n",
       "      <td>296156.00000</td>\n",
       "      <td>8303.420898</td>\n",
       "      <td>159177.484375</td>\n",
       "      <td>71481.867188</td>\n",
       "      <td>3.669561e+05</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1.0290</td>\n",
       "      <td>1.5</td>\n",
       "      <td>15661.307617</td>\n",
       "      <td>239330.390625</td>\n",
       "      <td>30706.921875</td>\n",
       "      <td>278924.68750</td>\n",
       "      <td>35697.226562</td>\n",
       "      <td>279872.7500</td>\n",
       "      <td>56019.222656</td>\n",
       "      <td>327893.5000</td>\n",
       "      <td>...</td>\n",
       "      <td>97762.851562</td>\n",
       "      <td>365059.93750</td>\n",
       "      <td>96441.281250</td>\n",
       "      <td>363170.93750</td>\n",
       "      <td>58987.832031</td>\n",
       "      <td>288542.56250</td>\n",
       "      <td>5880.359375</td>\n",
       "      <td>130922.351562</td>\n",
       "      <td>62098.503906</td>\n",
       "      <td>3.350240e+05</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1.5430</td>\n",
       "      <td>1.5</td>\n",
       "      <td>14526.572266</td>\n",
       "      <td>227596.468750</td>\n",
       "      <td>27759.197266</td>\n",
       "      <td>266642.81250</td>\n",
       "      <td>32271.853516</td>\n",
       "      <td>275117.9375</td>\n",
       "      <td>51267.171875</td>\n",
       "      <td>325726.9375</td>\n",
       "      <td>...</td>\n",
       "      <td>99005.960938</td>\n",
       "      <td>352817.65625</td>\n",
       "      <td>97583.656250</td>\n",
       "      <td>351476.68750</td>\n",
       "      <td>58434.101562</td>\n",
       "      <td>289191.40625</td>\n",
       "      <td>5149.017090</td>\n",
       "      <td>136740.640625</td>\n",
       "      <td>59760.777344</td>\n",
       "      <td>3.146491e+05</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>0.5144</td>\n",
       "      <td>2.0</td>\n",
       "      <td>36442.476562</td>\n",
       "      <td>552685.375000</td>\n",
       "      <td>57913.855469</td>\n",
       "      <td>754881.68750</td>\n",
       "      <td>64295.289062</td>\n",
       "      <td>780469.2500</td>\n",
       "      <td>93842.429688</td>\n",
       "      <td>892707.5000</td>\n",
       "      <td>...</td>\n",
       "      <td>130793.906250</td>\n",
       "      <td>845505.00000</td>\n",
       "      <td>129560.609375</td>\n",
       "      <td>842952.87500</td>\n",
       "      <td>87147.531250</td>\n",
       "      <td>708814.87500</td>\n",
       "      <td>50588.695312</td>\n",
       "      <td>621670.250000</td>\n",
       "      <td>153912.140625</td>\n",
       "      <td>1.021330e+06</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>1.0290</td>\n",
       "      <td>2.0</td>\n",
       "      <td>35045.921875</td>\n",
       "      <td>498700.343750</td>\n",
       "      <td>58710.476562</td>\n",
       "      <td>656219.56250</td>\n",
       "      <td>64407.519531</td>\n",
       "      <td>671388.8750</td>\n",
       "      <td>90042.953125</td>\n",
       "      <td>754463.6875</td>\n",
       "      <td>...</td>\n",
       "      <td>131359.234375</td>\n",
       "      <td>661598.06250</td>\n",
       "      <td>129916.445312</td>\n",
       "      <td>662650.75000</td>\n",
       "      <td>87981.367188</td>\n",
       "      <td>588829.75000</td>\n",
       "      <td>40036.625000</td>\n",
       "      <td>477864.031250</td>\n",
       "      <td>101340.085938</td>\n",
       "      <td>7.536669e+05</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 34 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "       P1   P5            P7             P8            P9           P10  \\\n",
       "0  0.5144  1.5  13768.680664  286696.812500  28747.720703  354003.71875   \n",
       "1  1.0290  1.5  15661.307617  239330.390625  30706.921875  278924.68750   \n",
       "2  1.5430  1.5  14526.572266  227596.468750  27759.197266  266642.81250   \n",
       "3  0.5144  2.0  36442.476562  552685.375000  57913.855469  754881.68750   \n",
       "4  1.0290  2.0  35045.921875  498700.343750  58710.476562  656219.56250   \n",
       "\n",
       "            P11          P12           P13          P14  ...            P29  \\\n",
       "0  34314.933594  357846.5000  56136.917969  395813.1875  ...   97280.148438   \n",
       "1  35697.226562  279872.7500  56019.222656  327893.5000  ...   97762.851562   \n",
       "2  32271.853516  275117.9375  51267.171875  325726.9375  ...   99005.960938   \n",
       "3  64295.289062  780469.2500  93842.429688  892707.5000  ...  130793.906250   \n",
       "4  64407.519531  671388.8750  90042.953125  754463.6875  ...  131359.234375   \n",
       "\n",
       "            P30            P31           P32           P33           P34  \\\n",
       "0  371894.75000   96071.804688  368650.34375  59487.933594  296156.00000   \n",
       "1  365059.93750   96441.281250  363170.93750  58987.832031  288542.56250   \n",
       "2  352817.65625   97583.656250  351476.68750  58434.101562  289191.40625   \n",
       "3  845505.00000  129560.609375  842952.87500  87147.531250  708814.87500   \n",
       "4  661598.06250  129916.445312  662650.75000  87981.367188  588829.75000   \n",
       "\n",
       "            P35            P36            P37           P38  \n",
       "0   8303.420898  159177.484375   71481.867188  3.669561e+05  \n",
       "1   5880.359375  130922.351562   62098.503906  3.350240e+05  \n",
       "2   5149.017090  136740.640625   59760.777344  3.146491e+05  \n",
       "3  50588.695312  621670.250000  153912.140625  1.021330e+06  \n",
       "4  40036.625000  477864.031250  101340.085938  7.536669e+05  \n",
       "\n",
       "[5 rows x 34 columns]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# 读取Excel文件，忽略前6行\n",
    "df0 = pd.read_csv('~/视频/20240322.csv', skiprows=6)\n",
    "df0 = df0.drop(df0.columns[[0,2,3,4,6]+list(range(-1, -5, -1))], axis=1)\n",
    "display(df0.head())\n",
    "df0_rows = int(df0.shape[0]/9)\n",
    "df0_cols = int(df0.shape[1]-2)\n",
    "df0_values = df0.values"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "90度缆绳1张力最值\n",
      "90度缆绳2张力最值\n",
      "135度缆绳2张力最值\n",
      "90度缆绳3张力最值\n",
      "135度缆绳3张力最值\n",
      "90度缆绳4张力最值\n",
      "135度缆绳4张力最值\n",
      "90度缆绳5张力最值\n",
      "135度缆绳5张力最值\n",
      "45度缆绳6张力最值\n",
      "90度缆绳6张力最值\n",
      "135度缆绳6张力最值\n",
      "45度缆绳7张力最值\n",
      "90度缆绳7张力最值\n",
      "135度缆绳7张力最值\n",
      "45度缆绳8张力最值\n",
      "90度缆绳8张力最值\n",
      "135度缆绳8张力最值\n",
      "90度缆绳9张力最值\n",
      "90度缆绳10张力最值\n",
      "90度缆绳11张力最值\n",
      "90度缆绳12张力最值\n",
      "90度缆绳13张力最值\n",
      "90度缆绳14张力最值\n",
      "90度缆绳15张力最值\n",
      "135度缆绳15张力最值\n",
      "45度缆绳16张力最值\n",
      "90度缆绳16张力最值\n",
      "135度缆绳16张力最值\n"
     ]
    }
   ],
   "source": [
    "# 定义插值函数\n",
    "def interpolate_surface(x, y, z):\n",
    "    # 创建网格点\n",
    "    xi = np.linspace(x.min(), x.max(), 100)\n",
    "    yi = np.linspace(y.min(), y.max(), 100)\n",
    "    xi, yi = np.meshgrid(xi, yi)\n",
    "    \n",
    "    # 进行插值\n",
    "    zi = griddata((x, y), z, (xi, yi), method='cubic')\n",
    "    return xi, yi, zi\n",
    "\n",
    "# 循环处理每个子图\n",
    "for j in range(df0_cols):\n",
    "    for i in range(df0_rows):\n",
    "        x = df0_values[i*9:(i+1)*9, 0].reshape(-1)\n",
    "        y = df0_values[i*9:(i+1)*9, 1].reshape(-1)\n",
    "        z = df0_values[i*9:(i+1)*9, j+2].reshape(-1) / 10000\n",
    "        \n",
    "        # 插值得到曲面\n",
    "        xi, yi, zi = interpolate_surface(x, y, z)\n",
    "        \n",
    "        # 创建一个三维坐标系\n",
    "        fig = plt.figure(figsize=(5,5))\n",
    "        ax = fig.add_subplot(111, projection='3d')\n",
    "\n",
    "        # 绘制曲面\n",
    "        ax.plot_surface(xi, yi, zi, cmap=cm.coolwarm)\n",
    "\n",
    "        jun_zui = '均值' if j%2==0 else '最值'\n",
    "        # 绘制与zi=50相交的曲面\n",
    "        # ax.contourf(xi, yi, zi, [50], colors='black', linestyles='solid', linewidths=3)\n",
    "        if np.any(zi >=49): \n",
    "            ax.contourf(xi, yi, zi, levels=[49.9,50.1], colors='black')\n",
    "        if np.any(zi >=195): \n",
    "            ax.contourf(xi, yi, zi, levels=[195.9,196.1], colors='green')\n",
    "            print(f'{i*45}度缆绳{int(j/2)+1}张力{jun_zui}')\n",
    "\n",
    "        # 绘制散点图\n",
    "        ax.scatter(x, y, z)\n",
    "\n",
    "        # 设置坐标轴标签\n",
    "        ax.set_xlabel('流速(m/s)')\n",
    "        ax.set_ylabel('波高(m)')\n",
    "        ax.set_zlabel('缆绳张力(t)')\n",
    "        plt.title(f'{i*45}度缆绳{int(j/2)+1}张力{jun_zui}')\n",
    "\n",
    "        plt.subplots_adjust(left=0.1, right=0.8, top=0.9, bottom=0.1)\n",
    "\n",
    "        # 保存图形\n",
    "        plt.savefig(f'/home/shuai/pic2/{i*45}度缆绳{int(j/2)+1}张力{jun_zui}.jpg')\n",
    "\n",
    "        # 关闭图形，以便下一次循环创建新的图形\n",
    "        plt.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>P1</th>\n",
       "      <th>P5</th>\n",
       "      <th>P39</th>\n",
       "      <th>P40</th>\n",
       "      <th>P41</th>\n",
       "      <th>P42</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0.5144</td>\n",
       "      <td>1.5</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1.0290</td>\n",
       "      <td>1.5</td>\n",
       "      <td>-0.002867</td>\n",
       "      <td>0.139097</td>\n",
       "      <td>1.370941</td>\n",
       "      <td>1.545274</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1.5430</td>\n",
       "      <td>1.5</td>\n",
       "      <td>-0.004080</td>\n",
       "      <td>0.134852</td>\n",
       "      <td>1.374937</td>\n",
       "      <td>1.532036</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>0.5144</td>\n",
       "      <td>2.0</td>\n",
       "      <td>0.217602</td>\n",
       "      <td>0.915120</td>\n",
       "      <td>1.349742</td>\n",
       "      <td>1.890763</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>1.0290</td>\n",
       "      <td>2.0</td>\n",
       "      <td>0.031626</td>\n",
       "      <td>0.335760</td>\n",
       "      <td>1.359569</td>\n",
       "      <td>1.723838</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "       P1   P5       P39       P40       P41       P42\n",
       "0  0.5144  1.5  0.000000  0.000000  0.000000  0.000000\n",
       "1  1.0290  1.5 -0.002867  0.139097  1.370941  1.545274\n",
       "2  1.5430  1.5 -0.004080  0.134852  1.374937  1.532036\n",
       "3  0.5144  2.0  0.217602  0.915120  1.349742  1.890763\n",
       "4  1.0290  2.0  0.031626  0.335760  1.359569  1.723838"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "5"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "4"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# 读取Excel文件，忽略前6行\n",
    "df1 = pd.read_csv('~/视频/20240322.csv', skiprows=6)\n",
    "df1 = df1.iloc[:, [1, 5]].join(df1.iloc[:, -4:])\n",
    "display(df1.head())\n",
    "df1_rows = int(df1.shape[0]/9)\n",
    "df1_cols = int(df1.shape[1]-2)\n",
    "df1_values = df1.values\n",
    "display(df1_rows, df1_cols)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 定义插值函数\n",
    "def interpolate_surface(x, y, z):\n",
    "    # 创建网格点\n",
    "    xi = np.linspace(x.min(), x.max(), 100)\n",
    "    yi = np.linspace(y.min(), y.max(), 100)\n",
    "    xi, yi = np.meshgrid(xi, yi)\n",
    "    \n",
    "    # 进行插值\n",
    "    zi = griddata((x, y), z, (xi, yi), method='cubic')\n",
    "    return xi, yi, zi\n",
    "\n",
    "# 循环处理每个子图\n",
    "for j in range(df1_cols):\n",
    "    for i in range(df1_rows):\n",
    "        x = df1_values[i*9:(i+1)*9, 0].reshape(-1)\n",
    "        y = df1_values[i*9:(i+1)*9, 1].reshape(-1)\n",
    "        z = df1_values[i*9:(i+1)*9, j+2].reshape(-1)\n",
    "        \n",
    "        # 插值得到曲面\n",
    "        xi, yi, zi = interpolate_surface(x, y, z)\n",
    "        \n",
    "        # 创建一个三维坐标系\n",
    "        fig = plt.figure(figsize=(5,5))\n",
    "        ax = fig.add_subplot(111, projection='3d')\n",
    "\n",
    "        # 绘制曲面\n",
    "        ax.plot_surface(xi, yi, zi, cmap=cm.coolwarm)\n",
    "\n",
    "        heng_zong = '横荡' if int(j/2)==0 else '纵荡'\n",
    "        jun_zui = '均值' if j%2==0 else '最值'\n",
    "        # 绘制与zi=50相交的曲面\n",
    "        if np.any(zi >=49): \n",
    "            ax.contourf(xi, yi, zi, levels=[49.9,50.1], colors='black')\n",
    "        if np.any(zi >=195): \n",
    "            ax.contourf(xi, yi, zi, levels=[195.9,196.1], colors='green')\n",
    "            print(f'{i*45}度船舶{heng_zong}{jun_zui}')\n",
    "\n",
    "        # 绘制散点图\n",
    "        ax.scatter(x, y, z)\n",
    "        # 设置坐标轴标签\n",
    "        ax.set_xlabel('流速(m/s)')\n",
    "        ax.set_ylabel('波高(m)')\n",
    "        ax.set_zlabel(f'{heng_zong}(m)')\n",
    "        plt.title(f'{i*45}度船舶{heng_zong}{jun_zui}')\n",
    "\n",
    "        plt.subplots_adjust(left=0.1, right=0.8, top=0.9, bottom=0.1)\n",
    "\n",
    "        # 保存图形\n",
    "        plt.savefig(f'/home/shuai/pic2/{i*45}度船舶{heng_zong}{jun_zui}.jpg')\n",
    "\n",
    "        # 关闭图形，以便下一次循环创建新的图形\n",
    "        plt.close()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "yolo_learn",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.18"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
